package mpo.serve;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import mpo.serve.presistence.Presistence;
import us.codecraft.webmagic.Spider;

public class MpoSpider {
	public static Map<String, Spider> spiders = new HashMap<String, Spider>();

	static {
		spiders.put("新浪",
				Spider.create(PageProcesserFactory.CreateSinaProcesser())
						.addPipeline(new Presistence()));

		spiders.put("搜狐",
				Spider.create(PageProcesserFactory.CreateSohuProcesser())
						.addPipeline(new Presistence()));

		spiders.put("腾讯",
				Spider.create(PageProcesserFactory.CreateTencentProcesser())
						.addPipeline(new Presistence()));

		spiders.put("网易",
				Spider.create(PageProcesserFactory.CreateNews163Processer())
						.addPipeline(new Presistence()));
	}

	public static String getStatus(String key) {
		Spider spider = (Spider) spiders.get(key);
		return spider.getStatus().name();
	}

	public static void startAll() {
		Set<String> keys = spiders.keySet();
		for (String key : keys) {
			start(key);
		}
	}

	public static void start(String key) {
		Spider spider = (Spider) spiders.get(key);
		spider.start();
	}

	public static void stopAll() {
		Set<String> keys = spiders.keySet();
		for (String key : keys) {
			stop(key);
		}
	}

	public static void stop(String key) {
		Spider spider = (Spider) spiders.get(key);
		spider.stop();
	}
}
